#include "head.h"

//买票
void buy_ticket(FlightSystem Flight_sys, U_data log_data, S_arr_list *ticket_list)
{
    
    int num;
    D_List_print(Flight_sys.Flight_list);
    printf("输入对应航班的班次进行购买:");
    scanf("%d",&num);
    system("clear");
    F_node *ptr = D_List_find_id(Flight_sys.Flight_list,num);
    printf("班次    机型        起点站  终点站  起飞时间    到达时间    飞行时间    票价  载客量  剩余座位  是否飞\n");
    printf("%-6d", ptr->data.id);
    printf("%-15s", ptr->data.flighttype);
    printf("%-10s", ptr->data.startcity);
    printf("%-10s", ptr->data.arrivecity);
    printf("%02d:%02d\t", ptr->data.starttime[0], ptr->data.starttime[1]);
    printf("%02d:%02d\t    ", ptr->data.arrivetime[0], ptr->data.arrivetime[1]);
    printf("%-10s\t", ptr->data.flytime);
    printf("%-6d", ptr->data.value);
    printf("%-8d", ptr->data.maxNUM);
    printf("%-8d  ", ptr->data.leftbuyersa);
    printf("%c\n", ptr->data.whetherfly);
    U_node *user_data = S_List_find_name(by_data_list(Flight_sys,log_data),log_data.name);
    //判断折扣
    double count = by_data_count(log_data);
    //判断余额
    printf("当前余额: %d\n",user_data->data.value);
    if (ptr->data.value * count > user_data->data.value)
    {
        printf("余额不足\n");
        press_enter_to_con();
        return;
    }
    if (ptr->data.leftbuyersa <= 0)
    {
        printf("座位不足\n");
        press_enter_to_con();
        return;
    }
    ptr->data.leftbuyersa--;
    user_data->data.value -= ptr->data.value *count;
    S_Arr_insert(ticket_list,1,ptr->data);
    strcpy(ticket_list->name[ticket_list->len-1].buf,log_data.name);
    S_Arr_File_print(ticket_list);
    printf("购票成功\n");
    printf("余额为%d\n",user_data->data.value);
    check_menu(ticket_list,log_data);
    press_enter_to_con(); 
}

//退票
void return_ticket(FlightSystem Flight_sys, U_data log_data, S_arr_list *ticket)
{
    check_menu(ticket,log_data);
    U_node *user_data = S_List_find_name(by_data_list(Flight_sys,log_data),log_data.name);
    double count = by_data_count(log_data);
    int num;
    int flag = 0;//定义是否有票
    printf("请选择您需要退的班次:");
    scanf("%d",&num);
    for (size_t i = 0; i < ticket->len; i++)
    {
        if (strcmp(log_data.name,ticket->name[i].buf) == 0)
        {
            flag = 1;
            if (ticket->data[i].id == num)
            {
                user_data->data.value += ticket->data[i].value * count;
                S_Arr_delete(ticket,i+1);
                printf("退票成功\n");
                printf("余额为%d\n",user_data->data.value);
                press_enter_to_con();
                return;
            }
        }
    }
    if (flag == 0)
    {
        printf("您并没有票\n");
        press_enter_to_con();
        return;
    }
    press_enter_to_con();
}

//余额充值
void balance_recharge(FlightSystem Flight_sys,U_data data)
{
    U_node *new_data = S_List_find_name(by_data_list(Flight_sys,data),data.name);
    printf("当前用户: %s 余额: %d\n",new_data->data.name,new_data->data.value);
    int money;
    printf("请输入您要充值的金额:");
    scanf("%d",&money);
    new_data->data.value = new_data->data.value + money;
    printf("充值成功，当前余额为%d\n",new_data->data.value);
    press_enter_to_con();
}

//查看航班
void check_flight(F_list *list)
{
    D_List_print(list);
    press_enter_to_con();
}

//修改密码
void set_passwd(FlightSystem Flight_sys,U_data data)
{
    U_node *new_data = S_List_find_name(by_data_list(Flight_sys,data),data.name);
    printf("当前用户: %s 密码: %s\n",new_data->data.name,new_data->data.passwd);
    printf("请输入新的密码:");
    scanf("%s",new_data->data.passwd);
    if (strcmp(data.passwd,new_data->data.passwd) == 0)
    {
        printf("新密码和旧密码不能一样\n");
        return;
    }
    printf("请重复输入新的密码:");
    scanf("%s",new_data->data.passwd);
    printf("密码修改成功 新密码为%s\n",new_data->data.passwd);
    press_enter_to_con();
}

//查看订单
void check_menu(S_arr_list *L, U_data data)
{
    for (size_t i = 0; i < L->len; i++)
    {
        if (strcmp(L->name[i].buf,data.name) == 0)
        {
            printf("您的订单如下:\n");
            printf("用户名: %s 班次: %d 机型: %s 起点站: %s 终点站: %s 起飞时间: %02d:%02d 到达时间: %02d:%02d 飞行时间: %s 票价: %d 载客量: %d 剩余座位: %d 是否飞: %c\n",
               L->name[i].buf, L->data[i].id, L->data[i].flighttype, L->data[i].startcity, L->data[i].arrivecity,
               L->data[i].starttime[0], L->data[i].starttime[1], L->data[i].arrivetime[0], L->data[i].arrivetime[1],
               L->data[i].flytime, L->data[i].value, L->data[i].maxNUM, L->data[i].leftbuyersa, L->data[i].whetherfly);
        }
        else
        {
            printf("您暂时没有出行的订单\n");
        }
    }
    return;
}